package com.taofuxn.yycreazy.util;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;

import java.io.File;

/**
 * Created by lol on 2016/1/4.
 */
public class PathUtil {

    /*初始化拍照图片保存路径*/
    public static String initFilePath(Context context) {
        String APP_IMAGE_PATH;
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            APP_IMAGE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath()
                    + "/com." + context.getPackageName() + "/image";
        } else {
            APP_IMAGE_PATH = Environment.getRootDirectory().getAbsolutePath()
                    + "/com." + context.getPackageName() + "/image";
        }
        File f = new File(APP_IMAGE_PATH);
        if (!f.exists()) {
            f.mkdirs();
        }
        return APP_IMAGE_PATH;
    }


    public static String initFilePath(Context context, String dir) {
        String APP_IMAGE_PATH;
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            APP_IMAGE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath()
                    + "/com." + context.getPackageName() + "/" + dir;
        } else {
            APP_IMAGE_PATH = Environment.getRootDirectory().getAbsolutePath()
                    + "/com." + context.getPackageName() + "/" + dir;
        }
        File f = new File(APP_IMAGE_PATH);
        if (!f.exists()) {
            f.mkdirs();
        }
        return APP_IMAGE_PATH;
    }

    public static String getPersonIconPath(String userId, Context context) {
        return initFilePath(context) + File.separator + userId + ".png";
    }

    public static String getPictureFilePath(String fileid, Context context, String DOWNTYPE) {
        return initFilePath(context) + File.separator + fileid + "." + DOWNTYPE;
    }

    public static String getAmrFilePath(String fileid, Context context, String DOWNTYPE) {
        return getAppRootPath(context) + "/recordAmr/" + fileid + "." + DOWNTYPE;
    }

    /**
     * 通知公告的附带路径
     *
     * @param context
     * @return
     */
    public static String getNoticeFilePath(Context context) {
        String path = getAppRootPath(context) + "/n_ce";
        File dir = new File(path);
        if (!dir.exists())
            dir.mkdirs();
        return path;
    }

    public static String getAppRootPath(Context context) {
        String APP_IMAGE_PATH;
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            APP_IMAGE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath()
                    + "/com." + context.getPackageName();
        } else {
            APP_IMAGE_PATH = Environment.getRootDirectory().getAbsolutePath()
                    + "/com." + context.getPackageName();
        }
        File f = new File(APP_IMAGE_PATH);
        if (!f.exists()) {
            f.mkdirs();
        }
        return APP_IMAGE_PATH;
    }

    /**
     * @param fileId 从本地获取档案文件，并且返回文件地址
     */
    public static String getRecordFromLacol(String fileId, Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (context.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED)
                return null;
        }
        String root = getAppRootPath(context);
        File rf = new File(root);
        File file[] = rf.listFiles();
        for (int i = 0; i < file.length; i++) {
            if (file[i].isDirectory()) {
                File enf[] = file[i].listFiles();
                for (int j = 0; j < enf.length; j++) {
                    if (enf[j].getName().startsWith(fileId)) {
                        return enf[j].getAbsolutePath();
                    }
                }
            }
        }
        return null;
    }

    /**
     * 根据type拿到文件地址
     *
     * @param type
     * @param context
     * @return
     */
    public static String getFileFromLocal(String type, Context context) {
        String root = getNoticeFilePath(context);
        File file = new File(root);
        if (file.exists()) {
            return root;
        }
        return null;
    }

    public static String getNewPhotoPath(Context context) {
        return getAppRootPath(context) + "/" + System.currentTimeMillis() + ".png";
    }



    public static Uri newPictureUri(String path) {
        return Uri.fromFile(new File(path));
    }

    private static boolean isFileExists(String path) {
        if (TextUtils.isEmpty(path)) {
            return false;
        }
        File f = new File(path);
        if (!f.exists()) {
            return false;
        }
        return true;
    }
}
